var mongoose = require('mongoose');
require('../model/dish.model');
var dishModel = mongoose.model('Dish');

module.exports = {
    findById: findById,
    findByType: findByType,
    findByName: findByName,
    findByBoth: findByBoth,
    findByMid: findByMid,
    addDish: addDish,
    deleteDish: deleteDish,
    updateDish: updateDish
};

/**
 * findById
 * @param userid
 * @param callBack
 */
function findById(id, callBack) {
    dishModel.find({
        _id: id
    }).exec(function (err, doc) {
        callBack(err, doc);
    });
}

/**
 * findByType
 * @param mid
 * @param type
 * @param callBack
 */
function findByType(mid, type, callBack) {
    dishModel.find({
        $and: [
            {mid: mid},
            {type: type}
        ]
    }).exec(function (err, docs) {
        callBack(err, docs);
    });
}

/**
 * findByName
 * @param mid
 * @param name
 * @param callBack
 */
function findByName(mid, name, callBack) {
    dishModel.find({
        $and: [
            {mid: mid},
            {name: {$regex: name}}
        ]
    }).exec(function (err, docs) {
        callBack(err, docs);
    });
}

/***
 * findByBoth
 * @param mid
 * @param type
 * @param name
 * @param callBack
 */
function findByBoth(mid, type, name, callBack) {
    dishModel.find({
        $and: [
            {mid: mid},
            {type: type},
            {name: {$regex: name}}
        ]
    }).exec(function (err, docs) {
        callBack(err, docs);
    });
}

/**
 * findByMid
 * @param mid
 * @param callBack
 */
function findByMid(mid, callBack) {
    dishModel.find({
        mid: mid
    }).exec(function (err, docs) {
        callBack(err, docs);
    });
}

/**
 * addDish
 * @param dish
 * @param callBack
 */
function addDish(dish, callBack) {
    dishModel.create(dish, callBack);
}

/**
 * deleteDish
 * @param id
 * @param callBack
 */
function deleteDish(id, callBack) {
    dishModel.remove({_id: id}, callBack);
}

/**
 * updateDish
 * @param dish
 * @param callBack
 */
function updateDish(dish, callBack) {
    dishModel.update({_id: dish._id}, dish, callBack);
}